﻿using BusinessLayer.DatabaseEntities;
using BusinessLayer.VDataEntities;
using BusinessLayer;
using CustomerServiceCenter.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CustomerServiceCenter.Helper;
using System.IO;


namespace CustomerServiceCenter.Controllers
{
    [WechatAuthentication]
    public class WeChatController : Controller
    {

        public ActionResult HomePage()
        {
            //此处需要获取用户名
            string type = Request.QueryString["type"];
            VPromoter EndUser = UserHelper.GetCurrentPromoterUser();
            List<TicketWeiXinModel> list = null;
            if (string.IsNullOrWhiteSpace(type) || null == type)
            {
                list = CustomerServiceCenter.Controllers.API.WeChatController.GetTickets(EndUser.ID, (int)TicketCurrentStatus.OnGoing);
            }
            else
            {
                list = CustomerServiceCenter.Controllers.API.WeChatController.GetTickets(EndUser.ID, (int)TicketCurrentStatus.History);
            }
            ViewData["Client"] = EndUser.ClientID + ".jpg";
            return View(list);
        }
        public ActionResult CreateApplication()
        {
            //string issueTypeID = Request.QueryString["IssueTypeID"];
            string applicationID = Request.QueryString["ApplicationID"];
            List<String> imagesList = API.WeChatController.imagesPath;

            if (null != imagesList && imagesList.Count > 0)
            {
                string EnduserID = UserHelper.GetCurrentPromoterUser().ID.ToString();
                for (int i = 0; i < imagesList.Count; i++)
                {
                    string[] paths = imagesList[i].Split('/');
                    if (paths.Contains(EnduserID))
                    {
                        imagesList.Remove(imagesList[i]);
                    }
                }
            }
            ViewData["ApplicationID"] = applicationID;
            return View();
        }

        public ActionResult TicketOperationInfo()
        {
            //此处需要获取用户名
            VPromoter EndUser = UserHelper.GetCurrentPromoterUser();
            ViewData["User"] = EndUser.ChineseName;
            Session["ticketID"] = Request.QueryString["ticketID"];
            if (Session["ticketID"] != null)
            {
                return View(CustomerServiceCenter.Controllers.API.WeChatController.GetTickets(EndUser.ID, 0, int.Parse(Session["ticketID"].ToString())).First());
            }
            else
            {
                return RedirectToAction("HomePage", "WeChat");
            }
        }


        public ActionResult EvaluatePage()
        {
            return View();
        }

        [HttpGet]
        public ActionResult EvaluatePage(int ticketId)
        {
            VPromoter EndUser = UserHelper.GetCurrentPromoterUser();
            ApplicationBusiness.GoToEvaluate(ticketId, Convert.ToInt32(TicketStatusType.Resolved), Convert.ToInt32(TicketStatusType.Resolved));
            LogHelper.Info(EndUser.ChineseName + "将ticketId = " + ticketId + "状态改为已解决");
            return View(CustomerServiceCenter.Controllers.API.WeChatController.GetTickets(EndUser.ID, (int)TicketStatusType.Resolved, ticketId).FirstOrDefault());
        }


        //获取当前用户的所有请求
        public List<TicketWeiXinModel> GetAllApplication()
        {
            //此处需要更改为登录的用户名
            //string EndUserName = Request.QueryString["username"];
            string EndUserName = UserHelper.GetCurrentPromoterUser().ChineseName;
            VTicketEntity[] tickets = BusinessLayer.TicketBusiness.GetTickets((UserHelper.GetCurrentPromoterUser().ID));
            List<TicketWeiXinModel> ticketsModel = new List<TicketWeiXinModel>();

            foreach (VTicketEntity ticket in tickets)
            {
                TicketWeiXinModel twxm = new TicketWeiXinModel();
                twxm.AssignerName = ticket.AssignerInfo.Name;
                twxm.AssignDate = ticket.AssignDate;
                twxm.ClientID = ticket.User.ClientID;
                twxm.IssueDescription = ticket.IssueDescription;
                twxm.IssueType = ProjectBusiness.GetIssueTypeInfoBySome(Convert.ToInt32(ticket.IssueType)).FirstOrDefault().IssueTypeDescription;
                twxm.Mobile = ticket.AssignerInfo.Mobile;
                twxm.OrderNumber = ticket.OrderNumber;
                twxm.Status = EnumDescripte.GetEnumDeScription((TicketStatusType)(ticket.Status));
                twxm.SubmitDate = ticket.SubmitDate;
                twxm.Telephone = ticket.AssignerInfo.Telephone;
                twxm.TicketID = ticket.TicketID;
                long[] applicationID = new long[] { ticket.ApplicationID };
                List<VApplication> applicationInfo = ProjectBusiness.GetApplicationInfoById(applicationID);
                if (null != applicationInfo && applicationInfo.Count > 0)
                {
                    twxm.OnCall = applicationInfo[0].OnCall;
                }
                ticketsModel.Add(twxm);
            }
            return ticketsModel;
        }


        #region 用户评价
        /// <summary>
        /// 用户评价
        /// </summary>
        /// <param name="ticketId"></param>
        /// <param name="ServiceRate"></param>
        /// <param name="ServiceComments"></param>
        /// <returns></returns>
        public void MakeComments(int ticketId, int ServiceRate, string ServiceComments)
        {
            ApplicationBusiness.MakeComments(ticketId, ServiceRate, ServiceComments, Convert.ToInt32(TicketStatusType.Closed), Convert.ToInt32(TicketStatusType.Closed));
            LogHelper.Info(UserHelper.GetCurrentPromoterUser().ChineseName + "对ticketId = " + ticketId + "进行评价并将状态改为关闭");
        }

        #endregion 用户评价结束






        ////微信用户修改Ticket状态
        public ActionResult ModifyStatus(string ticketID, string comments, string status)
        {
            VTicketStatus vs = new VTicketStatus();
            vs.actorName = UserHelper.GetCurrentPromoterUser().ChineseName;
            vs.Comments = comments;
            vs.ticketID = long.Parse(ticketID);
            vs.ticketStatus = int.Parse(status);

            TicketBusiness.ModifyTicketStatus(vs);
            LogHelper.Info(vs.actorName + EnumDescripte.GetEnumDeScription((TicketStatusType)vs.ticketStatus));
            return RedirectToAction("HomePage", "WeChat");
        }


        public ActionResult WriteFeedback()
        {
            
            ViewData["ClientID"] = UserHelper.GetCurrentPromoterUser().ClientID;
            return View();
        }



        //若用户没有信息，则需要登录
        public ActionResult Login()
        {
            return View();
        }

    }
}
